<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	public static $_script;
	/**
	 *
	 * @var string the default layout for the controller view. Defaults to
	 *      '//layouts/column1',
	 *      meaning using a single column layout. See
	 *      'protected/views/layouts/column1.php'.
	 */
	public $layout = '//layouts/column1';
	/**
	 *
	 * @var array context menu items. This property will be assigned to {@link
	 *      CMenu::items}.
	 */
	public $menu = array ();
	/**
	 *
	 * @var array the breadcrumbs of the current page. The value of this
	 *      property will
	 *      be assigned to {@link CBreadcrumbs::links}. Please refer to {@link
	 *      CBreadcrumbs::links}
	 *      for more details on how to specify this property.
	 */
	public $breadcrumbs = array ();

	/**
	 * (non-PHPdoc)
	 * @see CController::init()
	 */
	public function init()
	{
		parent::init();
		$this->scriptMap ();
		if (YII_DEBUG)
		{
			Yii::app()->clientScript->registerScriptFile('/jscript/plugins/debug.js');
		}
	}
	/**
	 * 
	 */
	public function onBeginRequest()
	{
		header ( "Content-type:text/html; charset=" . Yii::app ()->charset );
	}
	
	/**
	 */
	public function load()
	{
		// print_r('public function load()');
		$cs = Yii::app ()->getClientScript ();
		$cs->registerCoreScript ( 'jquery' );
		$basePath = Yii::getPathOfAlias ( 'zii.vendors.jui' );
		$baseUrl = Yii::app ()->getAssetManager ()->publish ( $basePath );
		$cs->registerScriptFile ( $baseUrl . '/js/jquery-ui.min.js' );
		$cs->registerCssFile ( '/css/jui/redmond/jquery-ui-1.8.2.custom.css' );
	}
	/**
	 *
	 * @return boolean
	 */
	public function scriptMap()
	{
		if (! YII_DEBUG)
		return false;
		// js,css文件存放目录
		$dir = array ('jscript','css');
		// $baseDir = dirname(Yii::app ()->basePath);
		// $dir1 = $this->scanDirs ( $baseDir . DIRECTORY_SEPARATOR . $dir [0] );
		// $dir2 = $this->scanDirs ( $baseDir . DIRECTORY_SEPARATOR . $dir [1] );
		// echo '<pre>';
		// print_r ( $dir1);
	}
	/**
	 * 
	 * Enter description here ...
	 * @param string $dir
	 */
	public function scanDirs($dir)
	{
		$output = array();
		$handle = opendir ( $dir );
		while ( false !== ($readdir = readdir ( $handle )) )
		{
			if ($readdir != '.' && $readdir != '..')
			{
				$path = $dir . DIRECTORY_SEPARATOR . $readdir;
				if (is_file ( $path ))
				{
					$extsion = strrchr ( $path, '.' );
					if (! in_array ( $extsion, array (
							'.css','.js' 
							) ))
							continue;
					$output [] = substr ( $path, 2, strlen ( $path ) );
				} elseif (is_dir ( $path ))
				{
					$output = $this->scanDirs ( $path );
					// $output [] = substr ( $path, 2, strlen ( $path ) ) . '/';
					// $output = array_merge ( $output, $this->build_tree (
					// $path ) );
				}
			}
		}
		closedir ( $handle );
		return $output;
	}

	public function get_leaf_dirs($dir)
	{
		$array = array ();
		$d = @dir ( $dir );
		if ($d)
		{
			while ( false !== ($entry = $d->read ()) )
			{
				if ($entry != '.' && $entry != '..')
				{
					$entry = $dir . DIRECTORY_SEPARATOR . $entry;
					if (is_dir ( $entry ))
					{
						$subdirs = self::get_leaf_dirs ( $entry );
						if ($subdirs)
						$array = array_merge ( $array, $subdirs );
						else
						$array [] = $entry;
					}
				}
			}
			$d->close ();
		}
		return $array;
	}
}